/*
 * @Author: your name
 * @Date: 2020-07-17 17:08:32
 * @LastEditTime: 2020-07-21 10:39:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_test\src\store\actions.js
 */ 
// import { encrypt } from "../common/js/crypto";
const PASS_PREFIX = "institutionEntry";
import { post, fetch, put } from "@/common/js/http";
// import Cookies from "js-cookie";
// import * as types from "./mutation-types";
import { Message } from 'element-ui';
import {
  login,
  loginOut,
} from "@/api/login";
const actions = {
  //登录
  async login({ commit, state }, payload) {
    const { code, data, msg, token } = await post(login, payload);
    if (code !== 200) {
      Message.error(msg)
      // Toast({
      //   message: msg || "登录失败",
      //   position: "bottom",
      //   duration: 5000
      // });
    } else {
      //存储token
      Message.success('登陆成功')
      sessionStorage.setItem(payload.username,JSON.stringify(data)) //该方法接受一个键名(key)和值(value)作为参数，将键值对添加到存储中；如果键名存在，则更新其对应的值。

    }
    return data;
  },
  // 退出
  async loginOut({ commit, state }) {
    await fetch(loginOut);
    window.localStorage.removeItem("token");
    Cookies.remove("JGRZTOKEN");
    commit(types.SET_USER_INFO, null);
    router.push("/login");
  },
  // GetSerialkey
  async GetSerialkey({ commit, state }, DataForm) {
    const { code, data, msg } = await fetch(GetSerialkey);
    if (code === 0) {
      return data;
    } else {
      MessageBox({
        type: "warning",
        title: "提示",
        message: msg || "获取Serialkey失败"
      });
    }
  },
  //获取信息
  async info({ commit, state }) {
    const { code, data, msg } = await fetch(info);
    if (code === 0) {
      return data;
    } else {
      MessageBox({
        type: "warning",
        title: "提示",
        message: msg || "获取Serialkey失败"
      });
    }
  },
  // 获取page
  async GetTableDable({ commit, state }, DataForm) {
    const { code, data, msg } = await fetch(
      page +
        "?&limit=" +
        DataForm.limit +
        "&certType=" +
        DataForm.type +
        "&page=" +
        DataForm.page +
        "&carNo=" +
        DataForm.carNo
    );
    if (code === 0) {
      return data;
    } else {
      MessageBox({
        type: "warning",
        title: "提示",
        message: msg || "登录失败"
      });
    }
  },
};

export default actions;
